package com.lwt.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lwt.gulimall.common.valid.AddGroup;
import com.lwt.gulimall.common.valid.UpdateGroup;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lwt.gulimall.product.entity.Brand;
import com.lwt.gulimall.product.service.BrandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwt.gulimall.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author liuweitao
 * @email liuweitao2541@gmail.com
 * @date 2024-01-26 16:11:43
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        return R.ok().put("page", brandService.getPage(params));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		Brand brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@Validated(value = AddGroup.class) @RequestBody Brand brand/*, BindingResult result*/){

        //没有做统一异常处理
/*        if(result.hasErrors()){
            Map<String,String> map = new HashMap<>();
            result.getFieldErrors().forEach(item ->{
                map.put(item.getField(),item.getDefaultMessage());
            });
            return R.error(400,"提交的数据不合法").put("data",map);
        }else {
            brandService.save(brand);
        }*/

        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@Validated(value = UpdateGroup.class) @RequestBody Brand brand){
        System.out.println("brand:"+ brand);
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
